﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Data.Common;
using System.Windows.Forms;
using System.Reflection;
using MySQLDriverCS;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
namespace stockfund.src
{
    class Tools
    {
     
        public static Hangyefund[] getHangyeFundFromHexun(string url)
        {
            WebClient myWebClient = new WebClient();
            byte[] myDataBuffer = myWebClient.DownloadData(url);
            string html = Encoding.Default.GetString(myDataBuffer);
            string[] objstrings = html.Replace("'","").Split(new string[]{"],["},StringSplitOptions.None);
            Hangyefund fund;
            int i = 1,n=0;
            List<string> ms = new List<string>();
            Hangyefund[] result = new Hangyefund[objstrings.Length-1];
            foreach (string objstring in objstrings)
            {
                if(i!=1 && i<120)
                {
                    fund = new Hangyefund();
                    string[] obj = objstring.Split(new string[] { "," }, StringSplitOptions.None);
                    fund.Rank = Int32.Parse(obj[0]);
                    fund.Hangye = obj[1];
                    fund.Url = obj[2];
                    fund.Hasstocks =Int32.Parse(obj[3]);
                    fund.Sumfund = Double.Parse(obj[4]);
                    fund.Infund = Double.Parse(obj[5]);
                    fund.Percent = Double.Parse(obj[6]);
                    result[n] = fund;
                    n++;
                }
                i++;
            }
            return result;
        }
        public static Stockinrank[] getStockInRankFromHexun(string url)
        {
            WebClient myWebClient = new WebClient();
            byte[] myDataBuffer = myWebClient.DownloadData(url);
            string html = Encoding.Default.GetString(myDataBuffer);
            string[] objstrings = html.Replace("'", "").Split(new string[] { "],[" }, StringSplitOptions.None);
            Stockinrank stock;
            int i = 1, n = 0;
            List<string> ms = new List<string>();
            Stockinrank[] result = new Stockinrank[objstrings.Length - 1];
            foreach (string objstring in objstrings)
            {
                if (i != 1 && i < 300)
                {
                    stock = new Stockinrank();
                    string[] obj = objstring.Split(new string[] { "," }, StringSplitOptions.None);
                    stock.Rank = Int32.Parse(obj[0]);
                    stock.Code = obj[1];
                    stock.Name = obj[2];
                    stock.inFund = Double.Parse(obj[4]);
                    stock.Percent = Double.Parse(obj[5]);
                    result[n] = stock;
                    n++;
                }
                i++;
            }
            return result;
        }
        public static Stockzcgg[] getStockZcggFromHexun(string url)
        {
            WebClient myWebClient = new WebClient();
            byte[] myDataBuffer = myWebClient.DownloadData(url);
            string html = Encoding.Default.GetString(myDataBuffer);

            html = html.Replace("\n", "").Replace("\r", "").Replace("\t", "").Replace("\"", "");
            string regexStr = "class=statisticsTable2>(?<content>.*?)</div>";
            Match mc = Regex.Match(html, regexStr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
            html = mc.Groups["content"].Value;
            string[] objstr = html.Split(new string[] { "</tr>" }, StringSplitOptions.RemoveEmptyEntries);
            int i = objstr.Length;
            Stockzcgg[] result = new Stockzcgg[i - 2];
            Stockzcgg stock;
            regexStr = "<tr>.*?target=_blank>(?<code>.*?)</a>.*?target=_blank>(?<name>.*?)</a>.*?<td align=right>(?<days>.*?)</td>.*?>(?<fund>.*?)</td>.*?<td align=right>(?<qujian>.*?)</td>.*?<td align=right>(?<liutong>.*?)</td>.*?<td align=right>(?<swap>.*?)</td>.*?<td align=right>(?<range>.*?)</td>.*?<td align=center>(?<time>.*?)</td>";
            for (int j = 1; j < i - 1; j++)
            {
                stock = new Stockzcgg();
                mc = Regex.Match(objstr[j], regexStr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                stock.Code = mc.Groups["code"].Value;
                stock.Name = mc.Groups["name"].Value;
                stock.Days = Int32.Parse(mc.Groups["days"].Value);
                stock.Fund = double.Parse(mc.Groups["fund"].Value);
                stock.Qujb = double.Parse(mc.Groups["qujian"].Value);
                stock.Liutb = double.Parse(mc.Groups["liutong"].Value);
                stock.Swap = double.Parse(mc.Groups["swap"].Value);
                stock.Range = double.Parse(mc.Groups["range"].Value);
                stock.Time = mc.Groups["time"].Value;
                result[j - 1] = stock;
            }
            return result;
        }
        public static string[] actions = new string[] { "boyistat","zcggstat","fundstat" };
        public static string[] urls = new string[] {"","","" };
        public static string getHtml(string url)
        {

            WebClient myWebClient = new WebClient();  
            byte[] myDataBuffer = myWebClient.DownloadData (url);
            return Encoding.Default.GetString(myDataBuffer);

        }
        public static MySQLConnection getConn()
        {

            MySQLConnection conn = null;
            try
            {
                conn = new MySQLConnection(new MySQLConnectionString("localhost", "stockstat", "root", "111111").AsString);
                conn.Open();
            }
            catch (Exception e)
            {
                throw new Exception("数据库没有启动，请确认！");
            }
            return conn;
        }
        public static bool checkData(MySQLConnection conn)
        {
            Tools.checkData(conn, "zcggstat");
            return false;
        }
        public static bool checkData(MySQLConnection conn,string table)
        {
            return true;
            DateTime curDate = DateTime.Now;
            int dayofweek = (int)curDate.DayOfWeek;
            DateTime cd = curDate;
            if (dayofweek == 6) cd.AddDays(-1);
            if (dayofweek == 7) cd.AddDays(-2);
            string cdsdf = cd.ToString("yyyy-MM-dd");
            if (Array.IndexOf(Tools.stop_2010, cdsdf) == -1 && cd.Hour >= 7)
            {
                int counter = 0;
                string sql = "select count(*) counter from " + table + " where statdate = '" + cdsdf + "'";
                MySQLCommand cmd = new MySQLCommand(sql, conn);

                counter = Int32.Parse(cmd.ExecuteScalar().ToString());
                if (counter > 0) return false;
                else
                {
                    switch (table)
                    {
                        case "fundstat":
                            (new HexunFund()).getData();
                            break;
                        case "boyistat":
                            (new HexunBoyi()).getData();
                            break;
                        case "zcggstat":
                            (new HexunZcgg()).getData();
                            break;
                    }
                    return true;
                }

            }
            else
            {
                return false;
            }

            return false;
        }
        public static string[] stop_2010 = new string[]{
            "2010-01-01",
            "2010-01-02",
            "2010-01-03",
            "2010-02-13",
            "2010-02-14",
            "2010-02-15",
            "2010-02-16",
            "2010-02-17",
            "2010-02-18",
            "2010-02-19",
            "2010-02-20",
            "2010-02-21",
            "2010-04-03",
            "2010-04-04",
            "2010-04-05",
            "2010-05-01",
            "2010-05-02",
            "2010-05-03",
            "2010-06-14",
            "2010-06-15",
            "2010-06-16",
            "2010-09-22",
            "2010-09-23",
            "2010-09-24",
            "2010-10-01",
            "2010-10-02",
            "2010-10-03",
            "2010-10-04",
            "2010-10-05",
            "2010-10-06",
            "2010-10-07"
        };
        public static string GetPYChar(string c)
        {
            byte[] array = new byte[2];
            array = System.Text.Encoding.Default.GetBytes(c);
            try
            {
                int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
                if (i < 0xB0A1) return "*";
                if (i < 0xB0C5) return "a";
                if (i < 0xB2C1) return "b";
                if (i < 0xB4EE) return "c";
                if (i < 0xB6EA) return "d";
                if (i < 0xB7A2) return "e";
                if (i < 0xB8C1) return "f";
                if (i < 0xB9FE) return "g";
                if (i < 0xBBF7) return "h";
                if (i < 0xBFA6) return "g";
                if (i < 0xC0AC) return "k";
                if (i < 0xC2E8) return "l";
                if (i < 0xC4C3) return "m";
                if (i < 0xC5B6) return "n";
                if (i < 0xC5BE) return "o";
                if (i < 0xC6DA) return "p";
                if (i < 0xC8BB) return "q";
                if (i < 0xC8F6) return "r";
                if (i < 0xCBFA) return "s";
                if (i < 0xCDDA) return "t";
                if (i < 0xCEF4) return "w";
                if (i < 0xD1B9) return "x";
                if (i < 0xD4D1) return "y";
                if (i < 0xD7FA) return "z";
            }
            catch (Exception e)
            {
                return "*";
            }
            
            return "*";
        }
        public static string GetPYString(string str)
        {
            str = str.Replace(" ","");
            string tempStr = "";
            foreach (char c in str)
            {
                if ((int)c >= 33 && (int)c <= 126)
                {//字母和符号原样保留                    
                    tempStr += c.ToString().ToLower();
                }
                else
                {//累加拼音声母
                    tempStr += GetPYChar(c.ToString());
                }
            }
            return tempStr;
        }
    }
    class HashMap : Hashtable
    {
        public override void Add(object key, object value)
        {
            if (this[key] == null)
            {
                base.Add(key, value);
            }
            else
            {
                this[key] = value;
            }
        }
    }
    class NoSortHashTable : Hashtable
    {
        private ArrayList keys = new ArrayList();
        public NoSortHashTable()
        {
        }
        public override void Add(object key, object value)
        {
            base.Add(key, value);
            keys.Add(key);
        }
        public override ICollection Keys
        {
            get
            {
                return keys;
            }
        }
        public override void Clear()
        {
            base.Clear();
            keys.Clear();
        }
        public override void Remove(object key)
        {
            base.Remove(key);
            keys.Remove(key);
        }
        public override IDictionaryEnumerator GetEnumerator()
        {
            return base.GetEnumerator();
        }
    }
}
